package qf.module.system.controller;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import qf.common.core.web.vo.AjaxResult;
import qf.common.logback.annotation.Log;
import qf.common.logback.enums.BusinessType;
import qf.common.logback.enums.OperatorType;
import qf.module.system.entitiy.SysUser;
import qf.module.system.service.SysUserService;
import qf.module.system.vo.SysUserVO;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;

@RestController
@RequestMapping("/system")
@Log4j2
public class SysUserController {

    @Resource
    private SysUserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    @Log(title = "系统授权",businessType = BusinessType.GRANT,operatorType = OperatorType.MANAGE)
    public AjaxResult loginUserByUserNameAndPassowrd(@PathParam(value = "userName") String userName,
                                                      @PathParam(value = "password") String password) {
        try {
            log.info("method:[loginTokenByUserNameAndPassowrd],params:{},{}",userName,password);
            SysUserVO sysUser = userService.loginTokenByUserNameAndPassowrd(userName,password);
            return AjaxResult.success(sysUser);
        }catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/oauth",method = RequestMethod.GET)
    @Log(title = "系统授权",businessType = BusinessType.GRANT,operatorType = OperatorType.MANAGE)
    public AjaxResult loginTokenByBusiNoAndSecretkey(@PathParam(value = "busiNo") String busiNo,
                                                     @PathParam(value = "secretKey") String secretKey){
        try {
            log.info("method:[loginTokenByBusiNoAndSecretkey],params:{},{}",busiNo,secretKey);
            SysUserVO sysUser = userService.loginTokenByBusiNoAndSecretkey(busiNo,secretKey);
            return AjaxResult.success(sysUser);
        }catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @Log(title = "新增用户",businessType = BusinessType.GRANT,operatorType = OperatorType.MANAGE)
    public AjaxResult saveUserEntity(@RequestBody SysUserVO sysUserVO){
        try {
            log.info("method:[saveUserEntity],params:{}",JSONObject.toJSONString(sysUserVO));
            SysUser sysUser = userService.save(sysUserVO);
            return AjaxResult.success(sysUser);
        }catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }
}
